﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HT.Data.BussinessLogic;
using HT.Data.Objects;
using HT.Data.Util;
using System.Data.SqlTypes;
using System.Text;
namespace Hotel.Operations
{
    public partial class Booking : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                txtStartDate.Text = DateTime.Now.Date.AddDays(-7).ToString("MM/dd/yyyy");
                txtEndate.Text = DateTime.Now.Date.AddDays(1).ToString("MM/dd/yyyy");
                LoadRoomType();
                GetBookings();
                BookingView.ActiveViewIndex = 0;
            }
        }

        public void LoadRoomType()
        {
            DataSet ds = HotelManger.GetInstance().GetAllRoomType(0, 0);
            ddlRoomType.DataSource = ds;
            ddlRoomType.DataValueField = "RoomTypeId";
            ddlRoomType.DataTextField = "RoomType";
            ddlRoomType.DataBind();
            ddlRoomType.Items.Insert(0, new ListItem("All", "0"));
            ddlBookingType.DataSource = ds;
            ddlBookingType.DataValueField = "RoomTypeId";
            ddlBookingType.DataTextField = "RoomType";
            ddlBookingType.DataBind();

        }

        public void GetBookings()
        {
            int roomid = (ddlRoomType.SelectedValue == "") ? 0 : Convert.ToInt32(ddlRoomType.SelectedValue);
            GridStatus.DataSource = HotelManger.GetInstance().GetAllBooking(txtRoomNumber.Text.Trim(), roomid, txtStartDate.Text, txtEndate.Text);
            GridStatus.DataBind();

        }

        public void GetAllData(int bookingid)
        {
           // int bookingid = string.IsNullOrEmpty(Request.QueryString["BookingId"]) ? 0 : Convert.ToInt32(Request.QueryString["BookingId"]);
            DataSet ds = HotelManger.GetInstance().GetAllInfo(bookingid);
            FillData(ds);
        }

        private void FillData(DataSet ds)
        {

            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    txtBookingNumber.Text = ds.Tables[0].Rows[0]["BookingNumber"].ToString();
                    ddlRoomType.SelectedValue = ds.Tables[0].Rows[0]["RoomTypeId"].ToString();
                    txtECheckInDate.Text = ds.Tables[0].Rows[0]["ExpectedCheckInDate"].ToString();
                    txtLengthOfStay.Text = ds.Tables[0].Rows[0]["LengthOfStay"].ToString();
                    chkPickUp.Checked = Convert.ToBoolean(ds.Tables[0].Rows[0]["PickupRequired"].ToString());
                    txtGuestName.Text = ds.Tables[0].Rows[0]["GuestName"].ToString();
                    txtEmail.Text = ds.Tables[0].Rows[0]["Email"].ToString();
                    txtPhone.Text = ds.Tables[0].Rows[0]["Phone"].ToString();
                    ddlPayMode.SelectedValue = ds.Tables[0].Rows[0]["AdvancePaymentModeId"].ToString();
                    txtAdvanceAmount.Text = ds.Tables[0].Rows[0]["AdvanceAmount"].ToString();
                    txtAdvancePaymentDate.Text = ds.Tables[0].Rows[0]["AdvancePaymentDate"].ToString();
                    txtAdvancePaymentSurcharge.Text = ds.Tables[0].Rows[0]["AdvancePaymentSurcharge"].ToString();
                    txtAdvancePaymentDescription.Text = ds.Tables[0].Rows[0]["AdvancePaymentDescription"].ToString();
                    CheckBoxAdvanceClearanceFlag.Checked = Convert.ToBoolean(ds.Tables[0].Rows[0]["AdvanceClearanceFlag"].ToString());
                    txtCancelReason.Text = ds.Tables[0].Rows[0]["CancelReason"].ToString();
                //    ddlCPaymentMode.SelectedValue = ds.Tables[0].Rows[0]["CancelFeePaymentModeId"].ToString();
                    txtCancellationFee.Text = ds.Tables[0].Rows[0]["CancellationFee"].ToString();
                    txtCancelFeeSurchage.Text = ds.Tables[0].Rows[0]["CalcelFeeSurchage"].ToString();
                  //  CheckBoxCancellationClearanceflag.Checked = Convert.ToBoolean(ds.Tables[0].Rows[0]["CancellationClearanceflag"].ToString());
                    // txtCancellationDa.Text = ds.Tables[0].Rows[0]["CancellationFee"].ToString();
                    // txtCancelFeeSurchage.Text = ds.Tables[0].Rows[0]["CalcelFeeSurchage"].ToString();



                }
            }

        }
        private void LoadPaymentMode()
        {
            DataSet ds = HotelManger.GetInstance().GetAllPaymentMode(0);

            if (ds.Tables[0].Rows.Count > 0)
            {
                ddlPayMode.DataSource = ds;
                ddlPayMode.DataTextField = "PaymentMode";
                ddlPayMode.DataValueField = "PaymentModeId";
                ddlPayMode.DataBind();

                ddlCPaymentMode.DataTextField = "PaymentMode";
                ddlCPaymentMode.DataValueField = "PaymentModeId";
                ddlCPaymentMode.DataBind();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                GetBookings();
            }
            catch (Exception ex)
            {
                StringBuilder objStringBuilder = new StringBuilder();
                objStringBuilder.Append("<script language='javascript'>");
                objStringBuilder.Append("alert('" + ex.Message + "')");
                objStringBuilder.Append("</script>");
                Page.RegisterStartupScript("ClientScript", objStringBuilder.ToString());
                //TODO:
                //Response.Write("<script type='text/javascript'>alert('" + ex.Message + "') </script>");
            }

        }

        private void Save()
        {
            try
            {
                HT.Data.Objects.Booking booking = new HT.Data.Objects.Booking();
                HotelManger.GetInstance().UpdateBooking(booking);
            }
            catch (Exception ex)
            {
            }
        }

        protected void GridStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetAllData(Convert.ToInt32(GridStatus.SelectedDataKey.Value.ToString()));
            LoadPaymentMode();
            BookingView.ActiveViewIndex = 1;
        }


    }
}